package test;

import com.fanml.xyz.HandleDataMart;
import com.fanml.xyz.HandleDbSql;
import com.fanml.xyz.HandleDbUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 一个简单的模板例子
 * <p>
 * Return:
 * Author: ChuanHui.Z
 * Date: 2020/10/20 21:22
 */
public class HandleTestDemo {


    private static String sql;
    private static String tableName = "DURL";//准备插入数据的表

    public static void main(String[] args) {
        int dataCount = 100000, pageSize = 1024, threadCount = 8;//抽取的总数，每页大小，线程数最大不超过32
        sql = "select name as name ,sex as xb from dual";//抽取的语句现阶段支持Oracle 抽取语句列名必须跟上别名否则报错，例如 select name as name ,sex as xb from dual
        DataSource sourceDataSource = null, targetDataSource = null;//源数据，目标数据源
        String className = "test.HandleTestDemo.HandleTestServer";//业务类所在全路径
        Map<String, Object> parameter = new HashMap<>();//业务类所需构造函数参数全部以Map形式传入，非必填项
        parameter.put("Name", "测试");
        try {
            if (dataCount > 50000) {
                //数据大于5W条开启线程
                HandleDbUtil.startThreadServer(dataCount, pageSize, threadCount, sql, sourceDataSource, targetDataSource, className, parameter);
            } else {
                HandleTestServer handleTestServer = new HandleTestDemo().new HandleTestServer(parameter);
                handleTestServer.setListData(HandleDbUtil.startSingleServer(sourceDataSource.getConnection(), sql));
                handleTestServer.setTargetConnection(targetDataSource.getConnection());
                handleTestServer.run();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //业务类
    class HandleTestServer extends HandleDataMart {

        private String name;

        HandleTestServer(Map<String, Object> args) {
            //构造函数
            name = args.get("Name").toString();
        }

        @Override
        protected void run() {
            List<Map<String, Object>> listData = getListData();//源数据
            PreparedStatement preparedStatement = null;
            if (listData != null && listData.size() > 0) {
                Connection targetConnection = getTargetConnection();//目标数据源
                System.out.println(name);
                try {
                    targetConnection.setAutoCommit(false);
                    HandleDbSql handleDbSql = HandleDbUtil.getHandleDbSql(sql, tableName);
                    String insertSql = handleDbSql.getInsertSql();//获取插入语句
                    List<String> listColumnName = handleDbSql.getListColumnName();//获取插入语句对应的列名
                    int columnCount = handleDbSql.getColumnCount();
                    preparedStatement = targetConnection.prepareStatement(insertSql);
                    for (int i = 0; i < listData.size(); i++) {
                        Map<String, Object> sourceMap = listData.get(i);
                        //TO_DO 入库业务逻辑
                        // Your coding...
                        //...
                        //...
                        for (int j = 0; j < columnCount; j++) {
                            String column_name = listColumnName.get(j);
                            preparedStatement.setObject(j + 1, sourceMap.get(column_name));
                        }
                        preparedStatement.addBatch();
                    }
                    preparedStatement.executeBatch();
                    targetConnection.commit();
                    targetConnection.setAutoCommit(true);
                } catch (Exception e) {
                    try {
                        targetConnection.rollback();
                    } catch (SQLException e1) {

                    }
                } finally {
                    if (preparedStatement != null) {
                        try {
                            preparedStatement.close();
                        } catch (SQLException e) {

                        }
                    }
                    if (targetConnection != null) {
                        try {
                            targetConnection.close();
                        } catch (SQLException e) {

                        }
                    }
                }
            }
        }
    }

}
